package com.sea.physical.service.impl;

import com.sea.physical.entity.*;
import com.sea.physical.mapper.CreatePlanMapper;
import com.sea.physical.service.*;
import com.sea.supply.entity.BillRefused;
import com.sea.supply.entity.DistributePlan;
import com.sea.supply.service.BillRefusedService;
import com.sea.supply.service.DistributePlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class CreatePlanServiceImpl implements CreatePlanService {

    @Autowired
    private CreatePlanMapper createPlanMapper;
    @Autowired
    private RecordService recordService;
    @Autowired
    private CreateDistributePlanService createDistributePlanService;
    @Autowired
    private DistributePlanService distributePlanService;
    @Autowired
    private CreateDespatchPlanService createDespatchPlanService;
    @Autowired
    private CreateDespatchSkuService createDespatchSkuService;
    @Autowired
    private PortService portService;
    @Autowired
    private StockProduceService stockProduceService;
    @Autowired
    private CreateSkuService createSkuService;
    @Autowired
    private CreatePackingSkuService createPackingSkuService;
    @Autowired
    private BillRefusedService billRefusedService;

    @Override
    public List<CreatePlan> findByCnd(CreatePlan createPlan) {
        return createPlanMapper.findByCnd(createPlan);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(CreatePlan createPlan) throws Exception {

        createPlan.setCreatePlanNo(createPlan.getCreatePlanNo().replaceAll("[\\p{C}\\p{Z}]", ""));
        //新增创建表头
        Integer res = createPlanMapper.insert(createPlan);
        createPlanMapper.updateReplaceTrim();
        //新增记录
        recordService.save(createPlan.getId(),1,"","");
        //新增分配表头
        CreateDistributePlan createDistributePlan=new CreateDistributePlan();
        createDistributePlan.setCreateId(createPlan.getId());
        createDistributePlanService.save(createDistributePlan);
        //新增预分配建议
        DistributePlan distributePlan=new DistributePlan();
        distributePlan.setCreateId(createPlan.getId());
        distributePlan.setPlanNo(createPlan.getCreatePlanNo());
        distributePlanService.save(distributePlan);

        //新增退税信息
        BillRefused bRefused = billRefusedService.findByRefusedPlanNo(createPlan.getCreatePlanNo());
        BillRefused billRefused=new BillRefused();

        if(null==bRefused) {
            billRefused.setRefusedPlanNo(createPlan.getCreatePlanNo());
            billRefusedService.save(billRefused);
        }
        return res;
    }


    @Override
    public CreatePlan findById(Integer id) {
        return createPlanMapper.findById(id);
    }

    @Override
    public int usave(CreatePlan createPlan) {
        return createPlanMapper.update(createPlan);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(Integer id) {
        CreatePlan createPlan = createPlanMapper.findById(id);
        if(createPlan.getCreatePlanStatus().equals(2)){
            throw new RuntimeException("请先取消分配，再删除柜子数据！");
        }
        return createPlanMapper.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTotal(Integer createId) {
        Integer boxNumber = createPackingSkuService.findBoxNumberByCreateId(createId);
        if (null == boxNumber) {
            return createPlanMapper.updateTotal(createId);
        } else {
            createPlanMapper.updateTotal(createId);
            return createPlanMapper.updateBoxNumberByCreateId(boxNumber, createId);
        }
    }

    @Override
    public int updateStatus(Integer status, Integer id) {
        int result = createPlanMapper.updateStatus(status, id);
        //判断是否入库
        if(status==7){
            CreateDespatchPlan createDespatchPlan = createDespatchPlanService.findByCreateId(id);
            //获取仓库ID
            Port port = portService.findById(createDespatchPlan.getDespatchStore());
            //判断是否海外仓
            if(StringUtils.isNotEmpty(port.getPortWare())&& port.getPortWare().equals("是")){
                List<CreateDespatchSku> list = createDespatchSkuService.findByCreateId(id);
                CreatePlan createPlan = findById(id);
                for(CreateDespatchSku createDespatchSku:list){
                    Integer createSkuId = createDespatchSku.getCreateSkuId();
                    CreateSku createSku = createSkuService.findById(createSkuId);
                    StockProduce stockProduce=new StockProduce();
                    stockProduce.setStockShipmentId(createSku.getCreateShipmentId());
                    stockProduce.setStockWareId(port.getId());
                    stockProduce.setStockWareName(port.getPortName());
                    stockProduce.setStockNo(createPlan.getCreatePlanNo());
                    stockProduce.setStockChannelName(createSku.getCreateChannelName());
                    stockProduce.setStockSku(createSku.getCreateSku());
                    stockProduce.setStockNumber(createDespatchSku.getCreateNumber());
                    stockProduceService.save(stockProduce);
                }
            }
        }
        return result;
    }

    @Override
    public CreatePlan findByCreateNo(String createNo) {
        return createPlanMapper.findByCreateNo(createNo);
    }

    @Override
    public List<CreatePlan> findRefusedPlanNo(Integer cou,String createPlanNo) {
        return createPlanMapper.findRefusedPlanNo(cou,createPlanNo);
    }

    @Override
    public int updateRefused(String createPlanNo) {
        return createPlanMapper.updateRefused(createPlanNo);
    }

    @Override
    public Map<String, Integer> logisticsStatus() {
        return createPlanMapper.logisticsStatus();
    }

    @Override
    public List<CreatePlan> findPaymentCreatePlanNo() {
        return createPlanMapper.findPaymentCreatePlanNo();
    }

    @Override
    public Integer updateQualityStatus(CreatePlan createPlan) {
        return createPlanMapper.updateQualityStatus(createPlan);
    }

    @Override
    public List<CreatePlan> findByQuality(CreatePlan createPlan) {
        return createPlanMapper.findByQuality(createPlan);
    }

    @Override
    public Integer updateCreateFile(String createFile, Integer id) {
        return createPlanMapper.updateCreateFile(createFile, id);
    }

    @Override
    public Integer updateBoxNumberByCreateId(Integer boxNumber, Integer id) {
        return createPlanMapper.updateBoxNumberByCreateId(boxNumber,id);
    }

    @Override
    public List<CreatePlan> findByCreatePlanNo(String[] ids) {
        return createPlanMapper.findByCreatePlanNo(ids);
    }
}
